import bluebird from 'bluebird';
import moment from 'moment';
import db from '../db';
import * as util from '../../../util';

function getCountByUserId(userId) {
    return db.get('xp_questionnaires').query(qb => {
        qb.where('createUserId', userId);
    }).count();
}

function getQuesByUserId(page, pageSize, query, user) {
    const {title, status, queTempId, isEnd} = query;
    const cols = [
        'xp_questionnaires.id', 'xp_questionnaires.queTempId',
        'xp_questionnairetemplates.title',
        'xp_questionnairetemplates.dateStart',
        'xp_questionnairetemplates.dateEnd',
        'xp_questionnairetemplates.remark',
        'xp_questionnaires.status',
        'xp_questionnaires.createTime',
        'xp_questionnaires.submitTime'
    ];
    return db.get('xp_questionnaires').query(qb => {
        qb.innerJoin('xp_questionnairetemplates', 'xp_questionnaires.queTempId', 'xp_questionnairetemplates.id');
        qb.where('xp_questionnaires.createUserId', user.id);
        //统计参与人数
        const t1 = db._.raw('select count(1) from xp_questionnaires where xp_questionnaires.queTempId=xp_questionnairetemplates.id ').wrap('(', ') totalUser');
        qb.select(t1);
        if (title) {
            qb.where('xp_questionnaires.title', 'like', title);
        }
        if (status) {
            qb.where('xp_questionnaires.status', '=', status);
        }
        if (queTempId) {
            qb.where('xp_questionnaires.queTempId', '=', queTempId);
        }
        if (isEnd !== undefined) {
            const operate = isEnd === '1' ? '<=' : '>';
            qb.where('xp_questionnairetemplates.dateEnd', operate, moment().format('YYYY-MM-DD HH:mm:ss'));
        }
        qb.orderBy('xp_questionnaires.createTime', 'desc');
    }).fetchPage({page, pageSize, columns: cols});
}

export function fetch(query, op, pageOpt) {
    return db.get('xp_questionnaires').query(qb => {
        if (query) {
            util.andWhere(query, op, qb);
        }
        qb.orderBy('createTime', 'desc');
    }).fetchPage({
        pageSize: pageOpt.pageSize,
        page: pageOpt.page
    });
}
export function find(query, op) {
    return db.get('xp_questionnaires').query(qb => {
        if (query) {
            util.andWhere(query, op, qb);
        }
        qb.orderBy('createTime', 'desc');
    }).list();
}
export function del(id) {
    return db.exec(model => model('xp_questionnaires').where({id}).list()
        .then(res => bluebird.reduce(res.toJSON(), (total, item) => model('xp_answers').where({queId: item.id}).drop()))
        .then(() => model('xp_questionnaires').where({id}).drop())
    );
}
export function add(data) {
    return db.exec(model => model('xp_questionnaires').forge().save(data, {method: 'insert'}));
}
export function edit(questionnaires) {
    return db.exec(model => model('xp_visitworkquestionnaires')
        .where({id: questionnaires.id}).save(questionnaires, {method: 'update', require: false}));
}

export {getQuesByUserId, getCountByUserId};
